package com.le.sarrs.web.controller

import com.le.sarrs.cms.CmsHandler
import com.le.sarrs.service.IRecService
import com.le.sarrs.service.SarrsService
import com.le.sarrs.service.impl.HoneyComRecService
import com.le.sarrs.util.HttpToolkit
import com.le.sarrs.util.IP4Country
import com.le.sarrs.util.SpringUtil
import com.le.sarrs.util.Utils
import com.le.sarrs.web.model.Location
import com.le.sarrs.web.model.RecommendParam
import net.sf.json.JSONObject
import org.apache.commons.lang3.StringUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
/**
 * 推荐主接口
 * Created by hexi on 2017/02/03.
 */
class RecGroovyController extends BaseGroovyController implements RecController {
    def Logger logger = LoggerFactory.getLogger(RecGroovyController.class)

    def IRecService recService = (HoneyComRecService) SpringUtil.get("honeyComRecService")
//    def env = (Environment) SpringUtil.get("reactorEnv")
//    def preactor = (Reactor) SpringUtil.get("preactor")
    /**
     * 获取附近留言列表
     * @return
     */
    @Override
    String getLeaveMsg(HttpServletRequest request, HttpServletResponse response) {

        def map = [:]
        map.put("pageNum", 1)
        map.put("pageSize", 10)
        map.put("listMsg", "test")
        return render.json(map)
    }

    /**
     * 影视版推荐
     * @return
     */
    @Override
    String recMovie(HttpServletRequest request, HttpServletResponse response) {

        def start = System.currentTimeMillis()

        final String cid = request.getParameter("cid")
        final String uid = request.getParameter("uid")
        final String lc = request.getParameter("lc")
        final String platform = request.getParameter("platform")
        final String appfrom = request.getParameter("appfrom")
        final String version = request.getParameter("version")
        String sessionid = request.getParameter("sessionid")
        String versiontype = request.getParameter("versiontype")
        final String lang = request.getParameter("lang")
        final String from = request.getParameter("from")
        final String brand = request.getParameter("brand")
        String enable_suggest_tag = request.getParameter("enable_suggest_tag")
        String suggest_tag = request.getParameter("suggest_tag")

        String ip = Utils.getIpAddr(request)
        String region = IP4Country.getCountry(ip)

        System.out.println("ip:" + ip + "region:" + region)

        StringBuffer sk = new StringBuffer()
        sk.append("http://127.0.0.1/sarrs?action=rec")

        sk.append("&cid=").append(cid)
        sk.append("&uid=").append(uid)
        sk.append("&lc=").append(lc)
        sk.append("&platform=").append(platform)
        sk.append("&appfrom=").append(appfrom)
        sk.append("&version=").append(version)
        sk.append("&sessionid=").append(sessionid)
        sk.append("&lang=").append(lang)
        sk.append("&from=").append(from)
        sk.append("&brand=").append(brand)
        sk.append("&enable_suggest_tag=").append(enable_suggest_tag)
        sk.append("&suggest_tag=").append(suggest_tag)
        sk.append("&versiontype=").append(versiontype)
        sk.append("&region=").append("CN")

        //todo test使用0602
        sk.append("&area=").append("rec_0603")

//        if (StringUtils.isNotBlank(from) && "auto_backseat,auto".contains(from)) {  //车载过滤live,外网
//            sk.append("&area=").append("rec_0813")
//        } else if (StringUtils.isBlank(suggest_tag)) {
//            sk.append("&area=").append("rec_0603")
//        } else {
//            sk.append("&area=").append("rec_0604")
//        }
        System.out.println("sk:" + sk.toString())

        def result = JSONObject.fromObject(HttpToolkit.sendGet(sk.toString()))
        List<Map<String, Object>> recList = SarrsService.getRecList(result, versiontype, !(StringUtils.isNotBlank(from) && "auto_backseat,auto".contains(from)))

        result.put("rec", recList)
        result.put("suggestTags", result.opt("suggest_tags"))
        result.remove("suggest_tags")
        //add置顶信息
        if ("0,hot".contains(cid)) {
            JSONObject jsonObject = CmsHandler.getData(versiontype, 1)
            if (jsonObject != null && jsonObject.optInt("status") == 1) {
                JSONObject tmp = jsonObject.optJSONObject("data")
                result.put("top", tmp.optJSONArray("records"))
            }
        }

        logger.info("|RecMovieLog|{}|{}|{}|{}|{}|", lc,uid,cid,System.currentTimeMillis() - start,result.opt("reid"))

        return result.toString()
    }

    @Override
    String recMovieByThrift(HttpServletRequest request, HttpServletResponse response) {

        def start = System.currentTimeMillis()

        final String cid = request.getParameter("cid")
        final String uid = request.getParameter("uid")
        final String lc = request.getParameter("lc")
        final String platform = request.getParameter("platform")
        final String appfrom = request.getParameter("appfrom")
        final String version = request.getParameter("version")
        String sessionid = request.getParameter("sessionid")
        String versiontype = request.getParameter("versiontype")
        final String lang = request.getParameter("lang")
        final String from = request.getParameter("from")
        final String brand = request.getParameter("brand")
        String enable_suggest_tag = request.getParameter("enable_suggest_tag")
        String suggest_tag = request.getParameter("suggest_tag")

        def params = new RecommendParam()
        params.setCid(cid)
        params.setLc(lc)
        params.setUid(uid)
        params.setPlatform(platform)
        params.setAppfrom(appfrom)
        params.setVersion(version)
        params.setSessionid(sessionid)
        params.setVersiontype(versiontype)
        params.setLang(lang)
        params.setFrom(from)
        params.setBrand(brand)
        params.setEnable_suggest_tag(enable_suggest_tag)
        params.setSuggest_tag(suggest_tag)

        String ip = Utils.getIpAddr(request)
        String region = IP4Country.getCountry(ip)

        Location location = new Location();
        location.setClientIp(ip);
        location.setRegion(StringUtils.isBlank(region) ? "CN" : region);
        location.setSalesArea(request.getParameter("sales_area"));
        location.setUserSettingCountry(request.getParameter("user_setting_country"));
        params.setLocation(location);
        params.setQueryString(request.getQueryString());

        def result = recService.queryRecommendation(params);

        System.out.println("ip:" + ip + "region:" + region)

        logger.info("|RecMovieByThriftLog|{}|{}|{}|{}|{}|", lc,uid,cid,System.currentTimeMillis() - start,result.opt("reid"))

        return result.toString()
    }


}
